<?php
namespace app\admin\controller;
use app\admin\model\Goods;

/**
 * 商品管理
 * Class Member
 */
class GoodsController extends BaseController {

    protected function _initialize() {
        parent::_initialize();
    }

    /**
     * 商品管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {		
		$map = [];
		$order = 'a.'.$param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map['a.'.$ziduan] = $keyword;
        }
		
        if (input('goods_type')) {
            $map['a.goods_type'] = input('goods_type');
        }
		
		$map['state'] = 0;
		$this->assign('ziduan', $ziduan);
        $goods_list = db('goods')->alias('a')
					->join('goodstype b','a.goods_type = b.id','LEFT')
					->field('a.*,b.name as typename')
        			->where($map)->order($order)->paginate(15, false, get_query());
					
    	$type_list = db('goodstype')->select();
        $this->assign('type_list',$type_list);
        return $this->fetch('index', ['goods_list' => $goods_list, 'keyword' => $keyword]);
    }
	
    /**
     * 添加商品
     * @return mixed
     */
    public function add() {
    	$type_list = db('goodstype')->select();
        $this->assign('type_list',$type_list);
        return $this->fetch();
    }

    /**
     * 保存商品
     */
    public function save() {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
			$data['add_time'] = now_time();
			$goods_model = new Goods();
            if ($goods_model->allowField(true)->save($data)) {
                $this->success('添加成功',url("index"));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑商品
     * @param $id
     * @return mixed
     */
    public function edit($id) {
       $goods= db("goods")->where('id',$id)->find();		
    	$type_list = db('goodstype')->select();
        $this->assign('type_list',$type_list);
        return $this->fetch('edit', ['goods' => $goods]);
    }

    /**
     * 更新商品
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
			$goods_model = new Goods();
            if ($goods_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                $this->success('更新成功',url("index"));
            } else {
                $this->error('更新失败');
            }
        }
    }
	
	//删除商品
	public function delete_item(){
		$data['state'] = 1;
		$res = db('goods')->where('id',input("id"))->update($data);
		if($res){
			 $this->success('删除成功',url("index"));
		}else{
			 $this->success('删除失败');
		}
	}
	
	/**
	 * 状态设置
	 * @return mixed
	 */
	public function status() {
		$ids = input('param.id/a');

		$val = input('param.val');
		$res = db('goods')->where('id','in', $ids)->setField('is_on_sale', $val);
		if ($res === false) {
			$this->error('设置失败');
		}
		$this->success('设置成功');
	}

}
